var data=[
    {choose:'<input type="checkbox"></input>',numbers:'<a>YCYY20180319102942699</a>',state:'提交',department:'新生儿小婴儿科',date1:'2018-04-16',date2:'2018-04-16',location:'医技科室'},
    {choose:'<input type="checkbox"></input>',numbers:'<a>YCYY20180319102942700</a>',state:'上报存档',department:'血液科',date1:'2018-07-16',date2:'2018-10-16',location:'手术麻醉'},
    {choose:'<input type="checkbox"></input>',numbers:'<a>YCYY20180319102942701</a>',state:'上报存档',department:'血液科',date1:'2022-04-16',date2:'2022-05-16',location:'医技科室'},
    {choose:'<input type="checkbox"></input>',numbers:'<a>YCYY20180319102942702</a>',state:'终止',department:'血液科',date1:'2018-04-16',date2:'2019-04-16',location:'手术麻醉'},
    {choose:'<input type="checkbox"></input>',numbers:'<a>YCYY20180319102942703</a>',state:'上报存档',department:'新生儿小婴儿科',date1:'2018-04-16',date2:'2018-04-16',location:'医技科室'},
    {choose:'<input type="checkbox"></input>',numbers:'<a>YCYY20180319102942704</a>',state:'上报存档',department:'康复科',date1:'2018-05-16',date2:'2018-04-16',location:'医技科室'},
    {choose:'<input type="checkbox"></input>',numbers:'<a>YCYY20180319102942705</a>',state:'上报存档',department:'康复科',date1:'2018-04-16',date2:'2018-04-16',location:'急诊'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942706',state:'提交',department:'康复科',date1:'2018-04-16',date2:'2018-04-16',location:'急诊'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942707',state:'提交',department:'肾脏免疫科',date1:'2018-04-16',date2:'2018-04-16',location:'门诊'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942708',state:'提交',department:'肾脏免疫科',date1:'2018-04-16',date2:'2018-04-20',location:'住院'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942709',state:'提交',department:'康复科',date1:'2018-04-16',date2:'2018-04-30',location:'住院'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942710',state:'终止',department:'新生儿小婴儿科',date1:'2018-04-16',date2:'2018-12-16',location:'门诊'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942711',state:'终止',department:'新生儿小婴儿科',date1:'2020-12-06',date2:'2021-04-16',location:'门诊'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942712',state:'终止',department:'康复科',date1:'2018-04-19',date2:'2018-04-16',location:'其他场所'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942713',state:'终止',department:'康复科',date1:'2018-04-16',date2:'2018-04-16',location:'场所不明'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942714',state:'提交',department:'血液科',date1:'2018-04-16',date2:'2018-04-16',location:'手术麻醉'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942715',state:'提交',department:'血液科',date1:'2018-04-16',date2:'2018-04-16',location:'手术麻醉'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942716',state:'上报存档',department:'康复科',date1:'2018-04-16',date2:'2018-04-16',location:'其他场所'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942717',state:'提交',department:'血液科',date1:'2018-04-16',date2:'2018-04-16',location:'医技科室'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942718',state:'终止',department:'新生儿小婴儿科',date1:'2018-04-16',date2:'2018-04-16',location:'急诊'},
    {choose:'<input type="checkbox"></input>',numbers:'YCYY20180319102942719',state:'上报存档',department:'康复科',date1:'2018-04-16',date2:'2018-04-16',location:'门诊'},
];
var page=1;
var count=7;
var oTBody=document.querySelector('tbody');
var oPagination = document.querySelector('.pagination');
var oPrevBtn = document.querySelector('.prev');
var oNextBtn = document.querySelector('.next');
var oSearchInp = document.querySelector('.searchInp');
var oSearchInp2 = document.querySelector('.searchInp2');
var oSearchInp3 = document.querySelector('.searchInp3');
var oSearchInp4 = document.querySelector('.searchInp4');
var oSearchInp5 = document.querySelector('.searchInp5');
var oSearchInp6 = document.querySelector('.searchInp6');
var oSearchInp7=document.querySelector('.searchInp7');
var oSearchInp8=document.querySelector('.searchInp8');
var oSearchBtn = document.querySelector('.searchBtn');
var oDel=document.querySelector('.del');
var oEditBtn=document.querySelector('.editStatus');
// 获取事件追踪按钮
var oEventBtn=document.querySelector('.eventTrace');
function render(){
    var str1 = oSearchInp.value;
    var str2 = oSearchInp2.value;
    var str3 = oSearchInp3.value;
    var str4 = oSearchInp4.value;
    var str5 = oSearchInp5.value==''?new Date('1992-8-25').getTime():new Date(oSearchInp5.value).getTime();//开始时间
    var str7 = oSearchInp7.value==''?new Date().getTime():new Date(oSearchInp5.value).getTime(); //结束时间
    var str6 = oSearchInp6.value==''?new Date('1992-8-25').getTime():new Date(oSearchInp5.value).getTime();//开始时间
    var str8 = oSearchInp8.value==''?new Date().getTime():new Date(oSearchInp5.value).getTime(); //结束时间
    var searchData=[];
    var data1=[];
    var data2=[];
    var data3=[];
    var data4=[];
    var data5=[];
    if(str1.trim()==''){
        data1=data;
    }else{
        data1=data.filter(function(v){
            return v.numbers.includes(str1)
        })
    }

    if(str2.trim()=='---请选择---'){
        data2=data1;
    }else{
        data2=data1.filter(function(v){
            return v.department.includes(str2)
        })
    }
    if(str3.trim()=='---请选择---'){
        data3=data2;
    }else{
        data3=data2.filter(function(v){
            return v.location.includes(str3)
        })
    }
    if(str4.trim()=='---请选择---'){
        data4=data3;
    }else{
        data4=data3.filter(function(v){
            return v.state.includes(str4)
        })
    }
    /* if(str5.trim()==''){
        data5=data4;
    }else{
        data5=data4.filter(function(v){
            return v.date1.includes(str5)
        })
    } */
    data5=data4.filter(function(v){
        var time=new Date(v.date1).getTime();
        return time>=str5 && time<=str7
    })
    searchData=data5.filter(function(v){
        var time=new Date(v.date2).getTime();
        return time>=str6 && time<=str8
    })
    /* if(str6.trim()==''){
        searchData=data5;
    }else{
        searchData=data5.filter(function(v){
            if(v.date2.includes(str6)){
                return v
            }
        })
    } */
    oTBody.innerHTML='';
    var renderArr=searchData.slice((page - 1) * count, page * count);
    for(var i=0;i<renderArr.length;i++){
        var oTr=document.createElement('tr');
        oTr.innerHTML=`
        <td>${renderArr[i].choose}</td>
        <td>${renderArr[i].numbers}</td>
        <td>${renderArr[i].state}</td>
        <td>${renderArr[i].department}</td>
        <td>${renderArr[i].date1}</td>
        <td>${renderArr[i].date2}</td>
        <td>${renderArr[i].location}</td>
        `;
        oTBody.appendChild(oTr);
    }
    var aPageBtn = document.querySelectorAll('.pageBtn');
    for (var i = 0; i < aPageBtn.length; i++) {
        oPagination.removeChild(aPageBtn[i].parentNode);
    }
    for (var i = 1; i <= Math.ceil(searchData.length / count); i++) {
        var oLi = document.createElement('li');
        oLi.innerHTML = `<a class="pageBtn" href="#">${i}</a>`;
        if (i == page) {
            oLi.classList.add('active');
        }
        oPagination.insertBefore(oLi, oPagination.lastElementChild);
    }
}
render();
oPagination.addEventListener('click',changePage);
function changePage() {
    if (event.target.className == 'pageBtn') {
        page = event.target.innerText;
        render();
    }
}
oPrevBtn.addEventListener('click', prev);
oNextBtn.addEventListener('click', next);
function prev() {
    if (page > 1) {
        page--;
        render();
    }
}
function next() {
    if (page < Math.ceil(data.length / count)) {
        page++;
        render();
    }
}
oSearchBtn.addEventListener('click', searchFn);
function searchFn() {
    page = 1;
    render();
}
function delFn(){
    oSearchInp.value='';
    oSearchInp2.value='---请选择---';
    oSearchInp3.value='---请选择---';
    oSearchInp4.value='---请选择---';
    oSearchInp5.value='';
    oSearchInp6.value='';
    render();
}
oDel.addEventListener('click',delFn);
oEditBtn.addEventListener('click', editStatus);
function editStatus() {
        $('#editStatus').modal('show');
}
oEventBtn.addEventListener('click', eventTrace);
function eventTrace() {
        $('#eventTrace').modal('show');
}